package com.stock.stockana.ui.fragments;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;

import com.stock.stockana.R;
import com.stock.stockana.constants.NetWorkConstants;

/**
 * Created by ljgsonx on 2016/3/4.
 */
public class NoticeWvFragment extends Fragment {

    private View mRootView;
    private WebView mWv;
    private ProgressBar mPb;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mRootView = inflater.inflate(R.layout.webview_layout, container, false);
        mWv = (WebView) mRootView.findViewById(R.id.stockNewsWv);
        mPb = (ProgressBar) mRootView.findViewById(R.id.pb);
        mWv.getSettings().setJavaScriptEnabled(true);
        mWv.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
        mWv.getSettings().setSupportMultipleWindows(true);
        mWv.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                //返回值是true的时候控制去WebView打开，为false调用系统浏览器或第三方浏览器
                view.loadUrl(url);
                return true;
            }
        });
        mWv.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                super.onProgressChanged(view, newProgress);
                mPb.setProgress(newProgress);
                if (newProgress == 100) {
                    mPb.setVisibility(View.GONE);
                }
            }
        });
        mWv.loadUrl(NetWorkConstants.NOTICE_URL);
        return mRootView;
    }

}
